package com.gai.shop.dao.impl;

import com.gai.shop.dao.CartDao;
import com.gai.shop.entity.Cart;
import com.gai.shop.entity.Product;
import com.gai.shop.util.DataBaseUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CartDaoImpl implements CartDao {
    private final QueryRunner QR = new QueryRunner();

    /**
     * 通过uid和pid来查找是否有购物车数据存在
     * @param uid
     * @param pid
     * @return
     * @throws Exception
     */
    @Override
    public Cart selectCartProductByUidAndPid(Integer uid, Integer pid) throws Exception {
        String sql = "SELECT p.`pid`,p.`pname`,p.`pinfo`,p.`pprice`,p.`pstate`,p.ptime,p.pimage,c.`cid`,c.`uid`,c.`ccount`,c.`cnum`" +
                "FROM product p,cart c WHERE p.`pid` = c.`pid` AND c.uid=? AND c.pid = ?;";

        Map<String, Object> query = QR.query(DataBaseUtil.getConnection(),
                sql,
                new MapHandler(),
                uid,
                pid);
        //如果没有查询到数据，就返回null
        if (query == null) {
            return null;
        }
        Cart cart = new Cart();
        Product product = new Product();
        //封装数据
        BeanUtils.populate(cart, query);
        BeanUtils.populate(product, query);
        cart.setProduct(product);
        return cart;
    }

    /**
     * 新增购物车数据
     * @param cart
     * @return
     * @throws Exception
     */
    @Override
    public Integer insertIntoCart(Cart cart) throws Exception {
        return QR.update(DataBaseUtil.getConnection(),
                "INSERT INTO cart VALUES (DEFAULT ,?,?,?,?);",
                cart.getUid(),
                cart.getProduct().getPid(),
                cart.getCcount(),
                cart.getCnum());
    }

    /**
     * 购物车数据修改
     * @param cart
     * @return
     * @throws Exception
     */
    @Override
    public Integer updateCartSetCnumAndCcount(Cart cart) throws Exception {
        return QR.update(DataBaseUtil.getConnection(),
                "UPDATE cart SET ccount=?,cnum=? WHERE uid = ? AND pid = ?;",
                cart.getCcount(),
                cart.getCnum(),
                cart.getUid(),
                cart.getProduct().getPid());
    }

    /**
     * 通过uid来查找是否有购物车数据存在
     * @param uid
     * @return
     * @throws Exception
     */
    @Override
    public List<Cart> selectCartProductByUidAndPid(Integer uid) throws Exception {
        String sql = "SELECT p.`pid`,p.`pname`,p.`pinfo`,p.`pprice`,p.`pstate`,p.ptime,p.pimage,c.`cid`,c.`uid`,c.`ccount`,c.`cnum`" +
                "FROM product p,cart c WHERE p.`pid` = c.`pid` AND c.uid=?;";
        List<Cart> cartList = new ArrayList<Cart>();
        //new MapListHandler() 返回list集合其中包含map和集合，每个map就代表着每一行数据
        List<Map<String, Object>> mapList = QR.query(DataBaseUtil.getConnection(),
                sql,
                new MapListHandler(),
                uid);
        if (mapList == null){
            return null;
        }
        for (Map<String, Object> query : mapList) {
            //声明需要封装的实体类
            Product product = new Product();
            Cart cart = new Cart();
            //工具封装
            BeanUtils.populate(cart,query);
            BeanUtils.populate(product,query);
            cart.setProduct(product);
            cartList.add(cart);
        }
        return cartList;
    }

    /**
     * 清空购物车
     * @param uid
     * @return
     * @throws Exception
     */
    @Override
    public Integer deleteCartByUid(Integer uid) throws Exception {
        return QR.update(DataBaseUtil.getConnection(),
                "DELETE FROM cart WHERE uid=?;",
                uid);
    }

    /**
     * 删除购物车数据
     * @param uid
     * @param pid
     * @return
     * @throws Exception
     */
    @Override
    public Integer deleteCartByUid(Integer uid, Integer pid) throws Exception {
        return QR.update(DataBaseUtil.getConnection(),
                "DELETE FROM cart WHERE uid = ? AND pid = ?;",
                uid,
                pid);
    }
}
